﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.Member
{
    public class PatientRecord : MemberBasePage
    {

        protected PatientRecordInfo PRInfo {get; set;}

        protected string StartDate {get; set;}
        protected string EndDate { get; set; }
        protected bool IsRecordCreated { get; set; }

        protected override void ShowPage()
        {
            base.ShowPage();
            PRInfo = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
            if (PRInfo == null || PRInfo.Id <= 0)
            {
                IsRecordCreated = false;
                PRInfo = new PatientRecordInfo();
                PRInfo.DiseaseId = 0;
                PRInfo.DiseaseName = "";
                PRInfo.PatientId = UserData.Id;
                StartDate = "";
                EndDate = "";
                PRInfo.Remark = "";
            }
            else 
            {
                IsRecordCreated = true;
                StartDate = PRInfo.StartDate.ToString("yyyy-MM-dd");
                EndDate = PRInfo.EndDate.ToString("yyyy-MM-dd");
            }
            if (IsPost) HandlePost();
        }
        
        private void HandlePost()
        {
            int diseaseid = RequestTools.GetFormInt("diseaseid", -1);
            string diseasename = RequestTools.GetFormString("diseasename");
            string startdate = RequestTools.GetFormString("startdate");
            string enddate = RequestTools.GetFormString("enddate");
            string remark = RequestTools.GetFormString("remark");

            



            if(IsRecordCreated == false)
            {
                if (diseaseid <= 0)
                {
                    this.AddErrLine("请选择疾病");
                    if (diseasename != "") this.AddErrLine("错误的疾病名称 " + diseasename);
                    return;
                }
                PRInfo.DiseaseId = diseaseid;
            }

            try
            {
                PRInfo.StartDate = Convert.ToDateTime(startdate);
            }
            catch
            {
                AddErrLine("开始时间无效");
                return;
            }

            try
            {
                PRInfo.EndDate = Convert.ToDateTime(enddate);
            }
            catch
            {
                AddErrLine("结束时间无效");
                return;
            }

            PRInfo.Remark = remark;
            PRInfo.UpdateDate = DateTime.Now;
            if (IsRecordCreated == false)
            {
                PRInfo.RecordDate = DateTime.Now;
                PatientBLL.Instance.AddPatientRecord(PRInfo);
                AddMsgLine("创建疾病记录成功");
                return;
            }
            else
            {
                PatientBLL.Instance.UpdatePatientRecord(PRInfo);
                AddMsgLine("更新疾病记录成功");
                return;
            }
        }

        
    }
}
